/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core.api;

import core.log.Monitor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author MartinLe
 */
public class API {

    public API() {
    }

    /**
     * Request api cua MingID.
     *
     * @param urlString
     * @param params
     * @return
     */
    public static String request(String urlString, String[] params) {
//        long processingTime = System.currentTimeMillis();
        BufferedReader in = null;
        String responseString = null;
        URLConnection connection = null;
//        /**
//         * ghi log
//         */
//        String param = "";
//        if (params != null && params.length > 0) {
//            for (int i = 0; i < params.length; i++) {
//                param += params[i] + " - ";
//            }
//        }
        if (urlString != null) {
            try {
                URL url = new URL(urlString);
                connection = url.openConnection();
                if (params.length > 0) {
                    connection.setDoOutput(true);
                    Writer post = new OutputStreamWriter(connection.getOutputStream());
                    for (int i = 0; i < params.length; i++) {
                        if (i > 0) {
                            post.write('&');
                        }
                        post.write(params[i]);
                    }
                    post.close();
                }
                connection.connect();
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                responseString = in.readLine();
            } catch (MalformedURLException ex) {
                Monitor.error("MalformedURLException", ex);
            } catch (FileNotFoundException ex) {
                Monitor.error("Failed to open stream to URL: ", ex);
            } catch (IOException ex) {
                Monitor.error("Error reading URL content: ", ex);
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    Monitor.error("close exception", ex);
                }
            }
        }
////        Reporter.debug(responseString);
//        processingTime = System.currentTimeMillis() - processingTime;
//        if (processingTime > Definition.SLOW_PROCESS_MSG_TIME) {
//            Reporter.logSlowedProcessCommand(0, processingTime, "log API call", param);
//        }
        return responseString;
    }

    public static String request(String url, Object... objects) {
        String s = "";
        try {
            String[] param = new String[objects.length];
            for (int i = 0; i < objects.length; i++) {
                Object object = objects[i];
                param[i] = (String) object;
            }
            s = request(url, param);
        } catch (Exception e) {
            Monitor.error("request error", e);
        } finally {
            return s;
        }
    }
}
